package com.sloth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sloth.mapper.AddressMapper;
import com.sloth.mapper.MyorderMapper;
import com.sloth.mapper.OrderDetailsMapper;
import com.sloth.mapper.ProductMapper;
import com.sloth.pojo.*;
import com.sloth.service.MyOrderService;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author lai
 * @time 2021/10/10 14:54
 */
@Service
public class MyOrderServiceImpl implements MyOrderService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private MyorderMapper myorderMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Override
    public void saveMyOrderAndOrderDetails(Myorder myorder, Map<Integer,Integer> proIdCount) {

        int rowsMyOrder = myorderMapper.insert(myorder);

        // 查询出当前订单的订单id，因为订单编号是根据当前时间生成的，是唯一的，直接通过订单编号查询就可以了
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andOrderNoEqualTo(myorder.getOrderNo());
        Myorder myorder1 = myorderMapper.selectByExample(myorderExample).get(0);

        // 从数据库中得到刚添加的订单记录
        if (rowsMyOrder == 1){

            Iterator<Map.Entry<Integer, Integer>> iterator = proIdCount.entrySet().iterator();
            while (iterator.hasNext()){

                Map.Entry<Integer, Integer> next = iterator.next();

                OrderDetails orderDetails = new OrderDetails();  // 用于向 mapper 中添加订单详情中的信息
                orderDetails.setOrderId(myorder1.getOrderId());  // 订单id
                orderDetails.setProId(next.getKey());  // 商品id
                orderDetails.setOdProCount(next.getValue());  // 对应商品id的数量
                orderDetails.setOdStates(0);  // 订单状态
                Calendar calendar = Calendar.getInstance();  // 创建的当前的时间对象
                calendar.setTime(new Date());  // 将当前时间放进对象中
                calendar.add(Calendar.DATE,4);  // 收货时间同一向后延长四天
                orderDetails.setOdHopeDate(calendar.getTime()); // 期望收货时间
                // 支付时间、商品出库时间、商品收获时间、商品完成时间都是空
                orderDetails.setState(0); // 未支付
                // 把新创建的订单详情添加到数据库中
                int rowsOrderDetail = orderDetailsMapper.insert(orderDetails);
            }
        }
    }

    // 根据订单编号、用户id，查找订单，返回下单页面，用于调用支付宝时向支付宝发送订单id，在AliPayController中根据订单id查询其他信息
    @Override
    public Integer getMyOrderIdByMyOrderNoAndCustomerId(Long orderNo, Integer customerId) {

        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andOrderNoEqualTo(orderNo).andCustIdEqualTo(customerId);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);

        return myorder.getOrderId();
    }


    // 巴用鑫
    @Override
    public ServerResponse getByCustomerIdAndOrder(Integer customerId, Integer odstate) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andCustIdEqualTo(customerId).andStateEqualTo(0);
        myorderExample.setOrderByClause("order_date DESC");



        //Page<Myorder>  page = PageHelper.startPage(pageNum, 5);
        List<Myorder> myorderList = myorderMapper.selectByExample(myorderExample);
        //分页
        // PageInfo pageInfo = page.toPageInfo();

        List<NewOrderDetailsVO> orderDetailVOArrayList = new ArrayList<>();
        //遍历我的订单
        for (Myorder myorder : myorderList) {
            //新建VO集合
            NewOrderDetailsVO orderDetailVO = new NewOrderDetailsVO();
            // g根据地址id查地址表
            Address address = addressMapper.selectByPrimaryKey(myorder.getAddressId());
            OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
            if (odstate != null) {
                orderDetailsExample.createCriteria().andOrderIdEqualTo(myorder.getOrderId()).andOdStatesEqualTo(odstate).andStateEqualTo(0);
                myorderExample.createCriteria().andStateEqualTo(0);}
            else{
                orderDetailsExample.createCriteria().andOrderIdEqualTo(myorder.getOrderId()).andStateEqualTo(0);
                myorderExample.createCriteria().andStateEqualTo(0); }

            List<OrderDetails> orderDetailslist = orderDetailsMapper.selectByExample(orderDetailsExample);
            // 把我的订单详情和商品存一个Map里，
            Map<OrderDetails, Product> orderDetailsProductMap = new HashMap<>();

            for (OrderDetails orderDetails : orderDetailslist) {
                Product product = productMapper.selectByPrimaryKey(orderDetails.getProId());
                orderDetailsProductMap.put(orderDetails, product);
            }

            orderDetailVO.setOrderDetailsProductMap(orderDetailsProductMap);
            orderDetailVO.setMyorder(myorder);
            orderDetailVO.setAddress(address);
            orderDetailVOArrayList.add(orderDetailVO);
            //  pageInfo.setList(orderDetailVOArrayList);

        }

        return ServerResponse.getSuccess(orderDetailVOArrayList);
    }

    //修改订单详情状态
    @Override
    public ServerResponse update(Integer myOrderId) {
        Myorder myorder = myorderMapper.selectByPrimaryKey(myOrderId);
        // 通过订单id在订单详情表中查询属于这个订单的所有订单详情
        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(myOrderId);



        if (myorder.getState()==0) {
            myorder.setState(1);

            int rows = myorderMapper.updateByPrimaryKey(myorder);
            if (rows>0)
                return ServerResponse.getSuccess("Success");
        }
        return ServerResponse.getFail(null);

    }

    //订单确认收货
    @Override
    public ServerResponse updatedicision(Integer odDetailsId) {
        OrderDetails orderDetails = orderDetailsMapper.selectByPrimaryKey(odDetailsId);
        System.out.println("订单详情id"+odDetailsId);
        if (orderDetails.getOdStates()==2) {
            orderDetails.setOdStates(3);
            orderDetails.setOdFinishDate(new Date());
            int rows = orderDetailsMapper.updateByPrimaryKey(orderDetails);
            if (rows>0)
                return ServerResponse.getSuccess("Success");
        }
        return ServerResponse.getFail(null);

    }

    // 根据订单编号查询订单信息
    @Override
    public Myorder getByMyOrderNO(Long myOrderNO) {

        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andOrderNoEqualTo(myOrderNO);

        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);

        return myorder;
    }

    // 根据订单编号查询订单信息
    @Override
    public Integer updateMyOrderStatue(Myorder myorder) {
        int rows = myorderMapper.updateByPrimaryKey(myorder);
        return rows;
    }

    // 根据订单id 查询订单信息
    @Override
    public Myorder getMyOrderByMyOrderID(Integer myOrderId) {

        Myorder myorder = myorderMapper.selectByPrimaryKey(myOrderId);

        return myorder;
    }


}